<template>
  <!-- 登录 -->
  <div style="width:500px;">
    <el-form :model="typeDate" :rules="rules" ref="loginForm" label-width="100px">
      <el-form-item label="编号" prop="name">
        <el-input v-model="typeDate.name" placeholder="请输入编号"></el-input>
      </el-form-item>
      <el-form-item label="租金" prop="money">
        <el-input v-model="typeDate.money" placeholder="请输入租金"></el-input>
      </el-form-item>
      <el-form-item label="车辆类型" prop="type_id">
        <el-select v-model="typeDate.type_id" placeholder="请选择">
          <el-option v-for="item in typeList" :key="item._id" :label="item.name" :value="item._id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车辆状态" prop="state">
        <el-select v-model="typeDate.state" placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { createBike } from "../../api/bike";
import { getType } from "../../api/type";
export default {
  name: "typeCreate",
  components: {},
  data () {
    return {
      typeList: [],
      typeDate: {},
      options: [
        {
          value: true,
          label: "正常",
        },
        {
          value: false,
          label: "报废",
        },
      ],
      rules: {
        name: [
          {
            required: true,
            message: "编号不能为空",
            trigger: "blur",
          },
        ],
        money: [
          {
            required: true,
            message: "租金不能为空",
            trigger: "blur",
          },
        ],
        state: [
          {
            required: true,
            message: "车辆状态不能为空",
            trigger: "blur",
          },
        ],
        type_id: [
          {
            required: true,
            message: "车辆状态不能为空",
            trigger: "blur",
          },
        ]
      },
    };
  },
  methods: {
    // 提交登录
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          createBike(this.typeDate).then((res) => {
            this.typeDate = {};
            this.$message({
              message: "添加成功",
              type: "success",
            });
          });
          // 发送请求
        } else {
          return false;
        }
      });
    },
  },
  mounted () {
    getType().then(res => {
      if (res.success) {
        this.typeList = res.result
      }
    })
  }
};
</script>
<style scoped></style>
